//
//  UIView+PhyMoVal.m
//  万州V7宝iOS客户端[ObjC]
//
//  Created by DimChum_Haul on 16/3/12.
//  Copyright © 2016年 沙罗双树831. All rights reserved.
//

#import "UIView+PhyMoVal.h"
#import "UIView+Geometry.h"

@implementation UIView (PhyMoVal)

#pragma mark - Helper * Core Animation 核心动画与工厂方法
#define __WeakSelf(Class) __weak typeof(&*Class) WS_Objc = self;
/** 抖动动画 */
static int numberOfShakes = 15;//震动次数
+ (CAKeyframeAnimation *)shakeAnimation:(CGRect)frame durationTime:(CGFloat)duration;
{
    CAKeyframeAnimation *shakeAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    CGMutablePathRef shakePath = CGPathCreateMutable();
    CGPathMoveToPoint(shakePath, NULL, CGRectGetMidX(frame), CGRectGetMidY(frame) );
    for (int index = 0; index < numberOfShakes; ++index)
    {
        CGPathAddLineToPoint(shakePath, NULL, CGRectGetMidX(frame) - arc4random_uniform(numberOfShakes*1.5),CGRectGetMidY(frame));
        
        CGPathAddLineToPoint(shakePath, NULL,  CGRectGetMidX(frame) + arc4random_uniform(numberOfShakes*1.5),CGRectGetMidY(frame));
    }
    
    CGPathCloseSubpath(shakePath);
    shakeAnimation.path = shakePath;
    shakeAnimation.duration = duration;
    CFRelease(shakePath);
    
    return shakeAnimation;
}

/** 点阵3次放大方法 */
+(void) DCHTinySlide:(UIView*)view{
    CABasicAnimation *basicAnimation = [[CABasicAnimation alloc] init];
    basicAnimation.keyPath = @"transform.scale";
    basicAnimation.toValue = @.25f;
    basicAnimation.repeatCount = 3;
    
    [view.layer addAnimation:basicAnimation forKey:nil];
}

static float startFValue = 1.33f;
+(void) DCHRotate:(UIView *)view withDuration:(CGFloat)duration
{
    float fuckingValue = .1f;
    view.alpha = 0.55f;
    
    if(duration <= 3.2f){
        duration += fuckingValue;
    }else{
        duration -= fuckingValue;
    }
    
    CATransform3D aniRotate = CATransform3DMakeRotation(M_PI, -1, 0, 0);
    
    [UIView animateWithDuration:duration animations:^{
        if(view.subviews.count > 1){
            view.layer.transform  = aniRotate;
        }
        view.alpha = 1.0f;
    } completion:^(BOOL finished) {
        startFValue -= fuckingValue;
        [UIView animateWithDuration:duration animations:^{
            view.transform = CGAffineTransformIdentity;
        }];
    }];
}

+ (CABasicAnimation*) DCHRotation_springRotation{
    CASpringAnimation *ani = [[CASpringAnimation alloc ] init];
    ani.keyPath = @"transform.rotation";
    ani.fromValue = @0.1f;
    ani.toValue = @12.2f;
    ani.duration = 1.77f;
    
    /// 秋千动画固有参数
    ani.mass = 3; //重力[0~1]
    ani.stiffness = 33; //阻尼[0~100]
    ani.damping = 0.25f; //衰减强度[0~10]
    ani.initialVelocity = 0.2f;
    return ani;
}

+(NSString*) Meta_Signature{
    return @"Dim.Chum_Haul in 重庆.江北区.汉庭酒店 2015.1.3 <Swift 2.0 :-> Animation_25_D_之基础动画类方法>";
}

+(void) TinyShake :(UIView*)view duration:(CGFloat)duration shakeTimes:(NSUInteger)SKNum{
    if (SKNum >= 15) { SKNum /= 2;}
    if (duration == 0.0f) duration = 0.75f;
    
    CGMutablePathRef pathRef = CGPathCreateMutable();
    CAKeyframeAnimation* KeyFrameAni = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    CGPathMoveToPoint(pathRef, nil, view.GeoCenterX, view.GeoCenterY);
    for (int i = 0 ; i < SKNum ;i++){
        CGPathAddLineToPoint(pathRef, nil, view.GeoCenterX + pow(SKNum - i, 1.1), view.GeoCenterY + i * 1.1);
        CGPathAddLineToPoint(pathRef, nil, view.GeoCenterX - pow(SKNum - i, 1.1), view.GeoCenterY - i * 1.1);
    }
    CGPathCloseSubpath(pathRef);
    KeyFrameAni.duration = duration;
    KeyFrameAni.path = pathRef;
    
    [view.layer addAnimation:KeyFrameAni forKey:nil];
}

+(void) PokerFace:(UIView*)view duration:(CGFloat)duration withPnt:(CGPoint)point{
    view.userInteractionEnabled = false;
    
    [UIView animateWithDuration:duration animations:^{
        CGAffineTransform flip = CGAffineTransformMakeScale(-1.5, -1.5);
        view.transform = flip;
        
        CGSize slicerSize = CGSizeMake(view.GeoWidth / 10, view.GeoWidth / 10);
        CGSize minusSlicer = CGSizeMake(point.x, point.y);
        
        /// 跳跃效果
        CGAffineTransform transPosition;
        if (CGSizeEqualToSize(CGSizeZero, minusSlicer)){
            transPosition = CGAffineTransformMakeTranslation(minusSlicer.width, minusSlicer.height);
            view.layer.affineTransform = transPosition;
        }else{
            transPosition = CGAffineTransformMakeTranslation(slicerSize.width, slicerSize.height);
            view.layer.affineTransform = transPosition;
        }
    } completion:^(BOOL finished) {
       [UIView animateWithDuration:duration/2 animations:^{
           view.layer.transform = CATransform3DIdentity;
       }completion:^(BOOL finished) {
           view.userInteractionEnabled = YES;
       }];
    }];
}

+ (CATransition *)animateWithType:(NSString *)type subType:(NSString *)subType duration:(CFTimeInterval)duration
{
    CATransition *animate = [CATransition animation];
    
    [animate setDuration:duration];
    [animate setType:type];
    [animate setSubtype:subType];
    [animate setTimingFunction:UIViewAnimationCurveEaseInOut];
    
    return animate;
}

+ (CABasicAnimation *)animateWithFromValue:(id)fromValue toValue:(id)toValue duration:(CFTimeInterval)duration keyPath:(NSString *)keyPath
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:keyPath];
    animation.fromValue = fromValue;
    animation.toValue   = toValue;
    animation.duration = duration;
    animation.repeatCount = 0;
    animation.autoreverses = NO;
    //以下两个设置，保证了动画结束后，layer不会回到初始位置
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    return animation;
}

+ (CAKeyframeAnimation *)animateWithBtnStatusChanged
{
    CAKeyframeAnimation *animate = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    
    animate.duration = 0.3;
    animate.removedOnCompletion = YES;
    animate.fillMode = kCAFillModeForwards;
    
    animate.values = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.7, 0.7, 1.0)],
                       [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)],
                       [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.8, 0.8, 1.0)],
                       [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    
    
    return animate;
}
@end


